package com.cain.calendar.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cain.calendar.R;
import com.cain.calendar.bean.Event;
import com.cain.calendar.tool.Constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

/**
 * Created by Cain on 2016/6/26.
 * 事件列表的view
 */
public class EventView extends LinearLayout {

    private Context context;

    public EventView(Context context) {
        super(context);
        this.context = context;
    }

    public EventView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public EventView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    /**
     * 刷新UI
     * @param date 一天的事件
     * @param listener event点击事件
     */
    public void update(ArrayList<Event> date, OnClickListener listener) {
        removeAllViews();

        HashMap<String, ArrayList<Event>> map = new HashMap<>();
        for (int i = 0; i < date.size(); ++i) {
            Event event = date.get(i);
            ArrayList<Event> events = map.get(event.getTag());
            if (events == null) {
                events = new ArrayList<>();
                map.put(event.getTag(), events);
            }
            events.add(event);
        }

        Set<String> keys = map.keySet();
        Iterator<String> iterator = keys.iterator();
        LayoutInflater inflater = LayoutInflater.from(context);
        Random random = new Random();
        while (iterator.hasNext()) {
            String key = iterator.next();
            ArrayList<Event> events = map.get(key);
            int colorId = Constant.colors[random.nextInt(Constant.colors.length)];

            View view = inflater.inflate(R.layout.event_tag, null);
            LinearLayout panel = (LinearLayout) view.findViewById(R.id.event_tag_view);
            View tagImage = view.findViewById(R.id.event_tag_image);
            TextView tagTextView = (TextView) view.findViewById(R.id.event_tag_name);
            tagImage.setBackgroundColor(Constant.getColor(context, colorId));
            tagTextView.setText(key);

            for (int i = 0; i < events.size(); ++i) {
                Event event = events.get(i);
                View item = inflater.inflate(R.layout.event_item, null);
                RelativeLayout button = (RelativeLayout) item.findViewById(R.id.event_item_button);
                ImageView pointImageView = (ImageView) item.findViewById(R.id.even_item_point);
                TextView timeTextView = (TextView) item.findViewById(R.id.event_item_time);
                TextView nameTextView = (TextView) item.findViewById(R.id.event_item_name);

                button.setTag(event);
                button.setOnClickListener(listener);
                GradientDrawable point = new GradientDrawable();
                point.setShape(GradientDrawable.OVAL);
                point.setColor(Constant.getColor(context, colorId));
                point.setSize(10, 10);
                pointImageView.setBackground(point);
                timeTextView.setText(String.format("%02d:%02d", event.getTime().getHours(), event.getTime().getMinutes()));
                nameTextView.setText(event.getName());

                panel.addView(item, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }

            LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 14, 0, 0);
            addView(panel, params);
        }
    }
}
